var API = API || {};

API.Form = {
    Settings: {
        date: new Date(),
        image: API.Data.base_url + "assets/img/calendar/calendar.png",
        limiter: 1
    },
    DatePicker: {
        OnSelect: function(dateStr) {

            var arrival = API.Form.DatePicker.GetDate(API.Form.Settings.start);
            var departure = API.Form.DatePicker.GetDate(API.Form.Settings.end);

            if (arrival >= departure) {

                var nextDayDate = API.Form.DatePicker.NextDayDate(arrival, 1);
                API.Form.DatePicker.SetMinDate(API.Form.Settings.end, nextDayDate);
                API.Form.DatePicker.SetDate(API.Form.Settings.end, nextDayDate);
            } else {
                API.Form.DatePicker.SetMinDate(API.Form.Settings.end, API.Form.DatePicker.NextDayDate(arrival, 1));

            }
        },
        SetDate: function(input, date) {
            $(input).datepicker('setDate', date);
        },
        SetMinDate: function(input, date) {
            $(input).datepicker('option', 'minDate', date);
        },
        GetDate: function(input, format) {
            return $(input).datepicker('getDate');
        },
        NextDayDate: function(date, noDays) {

            myDate = new Date(date);

            myDate.setDate(myDate.getDate() + noDays);
            return myDate;
        },
        Init: function(start, end, min_end_date) {

            API.Form.Settings.start = start;
            API.Form.Settings.end = end;

            $(start).datepicker({
                minDate: API.Form.DatePicker.NextDayDate(new Date(), API.Form.Settings.limiter),
                dateFormat: 'dd.mm.yy',
                showOn: 'both',
                buttonImage: API.Form.Settings.image,
                buttonImageOnly: true,
                nextText: '&raquo;',
                prevText: '&laquo;',
                //        dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
                showOtherMonths: true,
                firstDay: 1, // Start with Monday
                onSelect: API.Form.DatePicker.OnSelect
            });

            $(end).datepicker({
                minDate: API.Form.DatePicker.NextDayDate(new Date(), API.Form.Settings.limiter + min_end_date),
                dateFormat: 'dd.mm.yy',
                showOn: 'both',
                buttonImage: API.Form.Settings.image,
                buttonImageOnly: true,
                nextText: '&raquo;',
                prevText: '&laquo;',
                //        dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
                showOtherMonths: true,
                firstDay: 1 // Start with Monday
            })

            // SET DATES
            $(".checkinout").each(function() {
                $(this).datepicker('setDate', $(this).val());
            });

            // SET MIN DATES
            API.Form.DatePicker.OnSelect();

        }
    },
    AutoComplete: {
        Init: function(input, combo, plus_hotels) {

            // Here create url, default is FALSE (empty is FALSE, without hotels)
            if (plus_hotels === false) {
                plus_hotels = "";
            } else {
                plus_hotels = "/" + plus_hotels;

            }

            $(input).autocomplete({
                source: function(request, response) {
                    $.ajax({
                        url: API.Data.lang_url + "/destinations_hotels_JSON" + plus_hotels,
                        dataType: "jsonp",
                        data: {
                            featureClass: "P",
                            style: "full",
                            maxRows: 9,
                            name_startsWith: request.term
                        },
                        success: function(data) {
                            response($.map(data.items, function(item) {

                                return {
                                    label: item.name + (item.category == "child" ? " / " + item.parent : ""),
                                    value: item.name,
                                    category: item.category
                                };



                            }));
                        }
                    });
                },
                minLength: 0
            }).addClass("ui-widget ui-widget-content ui-corner-left").
                    data("ui-autocomplete")._renderItem = function(ul, item) {
                return $('<li class="ui-menu-item-with-icon"></li>')
                        .data("item.autocomplete", item)
                        .append('<a><span class="' + item.category + '-item-icon"></span>' + item.label + '</a>')
                        .appendTo(ul);
            };

            if (combo) {
                $("<a href='javascript:'>&nbsp;</a>")
                        .attr("title", API.Data.lang['form_show_all_items'])
                        .insertAfter($(input))
                        .addClass("custom-combo-link clear-border-left")
                        .click(function() {
                    // close if already visible                         
                    if ($(input).autocomplete("widget").is(":visible")) {
                        $(input).autocomplete("close");
                        return;
                    }
                    $(this).blur();
                    $(input).val("");
                    $(input).autocomplete("search", "");
                    $(input).focus();
                });
            }
        }

    }
};